﻿/******************************************************************************
 * 【本类功能概述】                                 					      *
 *  版权所有（C）2020-20XX，                                        *
 *  保留所有权利。                                                            *
 ******************************************************************************
 *  作者 : <hughjk>
 *  创建时间:2019/11/25 23:32:45
 *  文件描述: 创造
 *****************************************************************************/
using System;
using System.Collections.Generic;
using UnityEngine;
using Util;
namespace StateModel
{
    public class BarracksDrillState : State
    {
        public Soldier[] soldierAll;
        public int curSid;
        public Soldier curSoldier;
        [Header("当前训练时间")]
        public float _Cd;
        public float _Ccd;
        [Header("进度条")]
        public float _Progress;
       
        public Transform InitPos;
        public override void InitModel(Controller controller)
        {
            base.InitModel(controller);
            stateType = StateType.Drill;
            //InitPos.position = m_transform.position;
        }
        public override void StrState()
        {
            base.StrState();
            if (!isDrill())
            {
                SwitchState(StateType.Idle);
            }
        }

        public bool isDrill()
        {
            if (m_input.data.Execution.Count>0)
            {
                curSid = m_input.data.Execution[0]._Id; 
                curSoldier = Find(curSid);
                _Ccd = 0;
                _Cd = curSoldier._DrillTime;
                return true;
            }
            return false;
        }
        public Soldier Find(int id)
        {
            for (int i = 0; i < soldierAll.Length; i++)
            {
                if (id == soldierAll[i]._Id)
                {
                    return soldierAll[i];
                }
            }
            return null;
        }

        public override void OnUpdata(float dt)
        {
            base.OnUpdata(dt);
            _Ccd += dt;
            _Progress = _Ccd / _Cd;
            if (_Ccd >= _Cd)
            {
                var so = GameObject.Instantiate(curSoldier, transform.position,Quaternion.identity);
                so.gameObject.SetActive(true);
                so._TeamType = m_controller._TeamType;
                so.Init();
                so.m_input.data.IsTag = true;
                so.m_input.data.TagPos = InitPos.position;
                m_input.data.Execution.RemoveAt(0);
                if (!isDrill())
                {
                    SwitchState(StateType.Idle);
                }
            }
        }
    }
}

